<?php

class QuanHuyen_Model extends MyModel
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    function getQuanHuyen($maQuanHuyen)
    {
        $str = "SELECT maquanhuyen, tenquanhuyen, matinhthanh 
                FROM quanhuyen 
                WHERE maquanhuyen = '{$maQuanHuyen}'";
                
        $query = mysql_query($str);
        return $query;
    }
    
    function getAllQuanHuyen()
    {
        $str = "SELECT quanhuyen.maquanhuyen, quanhuyen.tenquanhuyen, tinhthanh.tentinhthanh
                FROM quanhuyen
                INNER JOIN tinhthanh
                ON quanhuyen.matinhthanh=tinhthanh.matinhthanh
                ORDER BY quanhuyen.maquanhuyen";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function getAllTinhThanh()
    {
        $str = "SELECT matinhthanh, tentinhthanh 
                FROM tinhthanh";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function checkExist($action, $maQuanHuyen, $tenQuanHuyen)
    {
        switch($action)
        {
            case "add":
                $str = "SELECT * 
                        FROM quanhuyen 
                        WHERE maquanhuyen = '{$maQuanHuyen}'";
                        
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                if($result == false)
                {
                    $str = "SELECT * 
                            FROM quanhuyen 
                            WHERE tenquanhuyen = '{$tenQuanHuyen}'";
                            
                    $query = mysql_query($str);
                    return mysql_num_rows($query) > 0 ? true : false;            
                }
                else 
                    return true;
                break;
                
            case "edit":
                $str = "SELECT * 
                        FROM quanhuyen 
                        WHERE tenquanhuyen = '{$tenQuanHuyen}'";
                        
                $query = mysql_query($str);
                return mysql_num_rows($query) > 0 ? true : false;
                break;
        }
    }
    
    function addQuanHuyen($maQuanHuyen, $tenQuanHuyen, $maTinhThanh)
    {
        $str = "INSERT INTO quanhuyen 
                VALUES('{$maQuanHuyen}', '{$tenQuanHuyen}', '{$maTinhThanh}')";
                
        mysql_query($str);
    }
    
    function editQuanHuyen($maQuanHuyen, $tenQuanHuyen, $maTinhThanh)
    {
        $str = "UPDATE quanhuyen 
                SET tenquanhuyen = '{$tenQuanHuyen}', matinhthanh = '{$maTinhThanh}'  
                WHERE maquanhuyen = '{$maQuanHuyen}'";
                
        mysql_query($str);
    }
    
    function checkQuanHuyenStatus($maQuanHuyen)
    {
        $str = "SELECT maquanhuyen 
                FROM phuongxa 
                WHERE maquanhuyen = '{$maQuanHuyen}'";
                
        $query = mysql_query($str);
        return mysql_num_rows($query) > 0 ? true : false;
    }
    
    function deleteQuanHuyen($maQuanHuyen)
    {
        $str = "DELETE FROM quanhuyen 
                WHERE maquanhuyen = '{$maQuanHuyen}'";
                
        mysql_query($str);
    }
    
}

?>